1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| export const useSignal = (initialState) => { const [state, setState] = useState(initialState) const stateRef = useRef(initialState)
const getState = () => stateRef.current
const editState = (newState) => { setState(newState)
if (typeof newState === 'function'){ stateRef.current = newState(stateRef.current) } else { stateRef.current = newState } }
return [state, editState, getState] }
|